Skip to content

feat: add HasItem expectation for collections#681

Merged
vbreuss merged 2 commits intomainfrom
topic/add-hasitem
Jul 19, 2025
Merged

feat: add HasItem expectation for collections#681
vbreuss merged 2 commits intomainfrom
topic/add-hasitem

Conversation

@vbreuss
Copy link
Copy Markdown
Member

@vbreuss vbreuss commented Jul 19, 2025

Have item at index

You can verify that the collection contains an item that satisfies the expectation on a given index (or any index).

IEnumerable<string> values = ["0th item", "1st item", "2nd item", "3rd item"];

await Expect.That(values).HasItem("1st item").AtIndex(1);
await Expect.That(values).HasItem(a => a.StartsWith("2nd")).AtAnyIndex();

Note: The same expectation works also for IAsyncEnumerable<T>.

@vbreuss vbreuss self-assigned this Jul 19, 2025
Copilot AI review requested due to automatic review settings July 19, 2025 17:54
@vbreuss vbreuss added the enhancement New feature or request label Jul 19, 2025
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This pull request introduces a new HasItem expectation for collection verification, allowing users to verify that collections contain elements matching specific criteria at given indices or any index. The implementation includes full support for both generic and non-generic enumerables, including async enumerables, and provides fluent API methods for index targeting.

  • Implementation of HasItem functionality for various collection types with predicate and object matching support
  • Addition of fluent API methods AtIndex() and AtAnyIndex() for targeted element verification
  • Comprehensive test coverage across different collection types and scenarios

Reviewed Changes

Copilot reviewed 33 out of 33 changed files in this pull request and generated no comments.

Show a summary per file
File Description
ThatEnumerable.HasItem.cs Core implementation of HasItem extension methods for synchronous collections
ThatAsyncEnumerable.HasItem.cs Async enumerable support for HasItem functionality
HasItemResult.cs Result type providing AtIndex/AtAnyIndex fluent methods
HasItemObjectResult.cs Enhanced result type with Equivalent/Using methods for object comparisons
CollectionIndexOptions.cs Index range validation and description logic
Various test files Extensive test coverage for all HasItem scenarios
Comments suppressed due to low confidence (16)

Tests/aweXpect.Tests/Collections/ThatEnumerable.HasItem.Tests.cs:38

  • The test method name 'WhenEnumerableContainsDifferentItemAtGivenIndex_ShouldSucceed' is misleading. Based on the test logic (lines 45-53), this test expects a failure/exception when the predicate doesn't match, so it should be named something like 'WhenEnumerableContainsDifferentItemAtGivenIndex_ShouldFail'.
			public async Task WhenEnumerableContainsDifferentItemAtGivenIndex_ShouldSucceed()

Tests/aweXpect.Tests/Collections/ThatEnumerable.HasItem.Tests.cs:68

  • The test method name 'WhenEnumerableContainsNoItemAtGivenIndex_ShouldSucceed' is misleading. The test expects a failure/exception (lines 80-88), so it should be named 'WhenEnumerableContainsNoItemAtGivenIndex_ShouldFail'.
			public async Task WhenEnumerableContainsNoItemAtGivenIndex_ShouldSucceed()

Tests/aweXpect.Tests/Collections/ThatEnumerable.HasItem.Tests.cs:195

  • The test method name 'WhenEnumerableContainsDifferentItemAtGivenIndex_ShouldSucceed' is misleading. The test expects a failure/exception (lines 205-213), so it should be named 'WhenEnumerableContainsDifferentItemAtGivenIndex_ShouldFail'.
			public async Task WhenEnumerableContainsDifferentItemAtGivenIndex_ShouldSucceed(

Tests/aweXpect.Tests/Collections/ThatEnumerable.HasItem.Tests.cs:233

  • The test method name 'WhenEnumerableContainsNoItemAtGivenIndex_ShouldSucceed' is misleading. The test expects a failure/exception (lines 245-253), so it should be named 'WhenEnumerableContainsNoItemAtGivenIndex_ShouldFail'.
			public async Task WhenEnumerableContainsNoItemAtGivenIndex_ShouldSucceed(int expected)

Tests/aweXpect.Tests/Collections/ThatEnumerable.HasItem.ImmutableTests.cs:16

  • The test method name 'WhenEnumerableContainsDifferentItemAtGivenIndex_ShouldSucceed' is misleading. The test expects a failure/exception, so it should be named 'WhenEnumerableContainsDifferentItemAtGivenIndex_ShouldFail'.
			public async Task WhenEnumerableContainsDifferentItemAtGivenIndex_ShouldSucceed()

Tests/aweXpect.Tests/Collections/ThatEnumerable.HasItem.ImmutableTests.cs:46

  • The test method name 'WhenEnumerableContainsNoItemAtGivenIndex_ShouldSucceed' is misleading. The test expects a failure/exception, so it should be named 'WhenEnumerableContainsNoItemAtGivenIndex_ShouldFail'.
			public async Task WhenEnumerableContainsNoItemAtGivenIndex_ShouldSucceed()

Tests/aweXpect.Tests/Collections/ThatEnumerable.HasItem.ImmutableTests.cs:112

  • The test method name 'WhenEnumerableContainsDifferentItemAtGivenIndex_ShouldSucceed' is misleading. The test expects a failure/exception, so it should be named 'WhenEnumerableContainsDifferentItemAtGivenIndex_ShouldFail'.
			public async Task WhenEnumerableContainsDifferentItemAtGivenIndex_ShouldSucceed()

Tests/aweXpect.Tests/Collections/ThatEnumerable.HasItem.ImmutableTests.cs:142

  • The test method name 'WhenEnumerableContainsNoItemAtGivenIndex_ShouldSucceed' is misleading. The test expects a failure/exception, so it should be named 'WhenEnumerableContainsNoItemAtGivenIndex_ShouldFail'.
			public async Task WhenEnumerableContainsNoItemAtGivenIndex_ShouldSucceed()

Tests/aweXpect.Tests/Collections/ThatEnumerable.HasItem.EnumerableTests.cs:39

  • The test method name 'WhenEnumerableContainsDifferentItemAtGivenIndex_ShouldSucceed' is misleading. The test expects a failure/exception, so it should be named 'WhenEnumerableContainsDifferentItemAtGivenIndex_ShouldFail'.
			public async Task WhenEnumerableContainsDifferentItemAtGivenIndex_ShouldSucceed()

Tests/aweXpect.Tests/Collections/ThatEnumerable.HasItem.EnumerableTests.cs:69

  • The test method name 'WhenEnumerableContainsNoItemAtGivenIndex_ShouldSucceed' is misleading. The test expects a failure/exception, so it should be named 'WhenEnumerableContainsNoItemAtGivenIndex_ShouldFail'.
			public async Task WhenEnumerableContainsNoItemAtGivenIndex_ShouldSucceed()

Tests/aweXpect.Tests/Collections/ThatEnumerable.HasItem.EnumerableTests.cs:191

  • The test method name 'WhenEnumerableContainsDifferentItemAtGivenIndex_ShouldSucceed' is misleading. The test expects a failure/exception, so it should be named 'WhenEnumerableContainsDifferentItemAtGivenIndex_ShouldFail'.
			public async Task WhenEnumerableContainsDifferentItemAtGivenIndex_ShouldSucceed(

Tests/aweXpect.Tests/Collections/ThatEnumerable.HasItem.EnumerableTests.cs:231

  • The test method name 'WhenEnumerableContainsNoItemAtGivenIndex_ShouldSucceed' is misleading. The test expects a failure/exception, so it should be named 'WhenEnumerableContainsNoItemAtGivenIndex_ShouldFail'.
			public async Task WhenEnumerableContainsNoItemAtGivenIndex_ShouldSucceed(int expected)

Tests/aweXpect.Tests/Collections/ThatAsyncEnumerable.HasItem.Tests.cs:39

  • The test method name 'WhenEnumerableContainsDifferentItemAtGivenIndex_ShouldSucceed' is misleading. The test expects a failure/exception, so it should be named 'WhenEnumerableContainsDifferentItemAtGivenIndex_ShouldFail'.
			public async Task WhenEnumerableContainsDifferentItemAtGivenIndex_ShouldSucceed()

Tests/aweXpect.Tests/Collections/ThatAsyncEnumerable.HasItem.Tests.cs:69

  • The test method name 'WhenEnumerableContainsNoItemAtGivenIndex_ShouldSucceed' is misleading. The test expects a failure/exception, so it should be named 'WhenEnumerableContainsNoItemAtGivenIndex_ShouldFail'.
			public async Task WhenEnumerableContainsNoItemAtGivenIndex_ShouldSucceed()

Tests/aweXpect.Tests/Collections/ThatAsyncEnumerable.HasItem.Tests.cs:190

  • The test method name 'WhenEnumerableContainsDifferentItemAtGivenIndex_ShouldSucceed' is misleading. The test expects a failure/exception, so it should be named 'WhenEnumerableContainsDifferentItemAtGivenIndex_ShouldFail'.
			public async Task WhenEnumerableContainsDifferentItemAtGivenIndex_ShouldSucceed()

Tests/aweXpect.Tests/Collections/ThatAsyncEnumerable.HasItem.Tests.cs:220

  • The test method name 'WhenEnumerableContainsNoItemAtGivenIndex_ShouldSucceed' is misleading. The test expects a failure/exception, so it should be named 'WhenEnumerableContainsNoItemAtGivenIndex_ShouldFail'.
			public async Task WhenEnumerableContainsNoItemAtGivenIndex_ShouldSucceed()

## Have item at index

You can verify that the collection contains an item that satisfies the expectation on a given index (or any index).

```csharp
IEnumerable<string> values = ["0th item", "1st item", "2nd item", "3rd item"];

await Expect.That(values).HasItem("1st item").AtIndex(1);
await Expect.That(values).HasItem(a => a.StartsWith("2nd")).AtAnyIndex();
```

*Note: The same expectation works also for `IAsyncEnumerable<T>`.*
@vbreuss vbreuss force-pushed the topic/add-hasitem branch from dc2748d to 15cc4da Compare July 19, 2025 17:56
@vbreuss vbreuss enabled auto-merge (squash) July 19, 2025 18:00
@sonarqubecloud
Copy link
Copy Markdown

@github-actions
Copy link
Copy Markdown
Contributor

Test Results

    4 files   -     34      4 suites   - 34   22s ⏱️ - 2m 11s
  897 tests  - 12 986    896 ✅  - 12 985  1 💤  - 1  0 ❌ ±0 
2 577 runs   - 35 946  2 576 ✅  - 35 945  1 💤  - 1  0 ❌ ±0 

Results for commit ebd8cf9. ± Comparison against base commit 5a2769a.

@vbreuss vbreuss merged commit a7ec361 into main Jul 19, 2025
13 checks passed
@vbreuss vbreuss deleted the topic/add-hasitem branch July 19, 2025 18:06
@github-actions
Copy link
Copy Markdown
Contributor

This is addressed in release v2.20.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request state: released The issue is released

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants